package com.zmn.oms.business.impl.tipsconfig;

import com.alibaba.nacos.common.utils.Objects;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.interfaces.tipsconfig.PictureTipsConfigBService;
import com.zmn.oms.model.dio.conf.tips.PictureTipsConfigDTO;
import com.zmn.oms.model.entity.tipsconfig.PictureTipsConfig;
import com.zmn.oms.services.interfaces.tipsconfig.PictureTipsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class PictureTipsConfigBServiceImpl implements PictureTipsConfigBService {
    @Autowired
    private PictureTipsConfigService pictureTipsConfigService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAdd(PictureTipsConfigDTO tipsConfig, String operator) {
        if (Objects.isNull(tipsConfig)) {
            return;
        }
        // 需要删除的配置ID
        if (CollectionUtil.isNotNullOrEmpty(tipsConfig.getDelListId())) {
            pictureTipsConfigService.deleteByKeys(tipsConfig.getDelListId());
        }

        Integer sort = Optional.ofNullable(pictureTipsConfigService.findMaxSort(tipsConfig.getServCategId(), tipsConfig.getCategOneId(), tipsConfig.getCategId())).orElse(GlobalConsts.NONE);
        for (PictureTipsConfig tips : tipsConfig.getTipsConfigList()) {
            tips.setSort(sort + 1);
            tips.setCreater(operator);
            tips.setUpdater(operator);
            sort++;
        }
        pictureTipsConfigService.insertPictureTipsList(tipsConfig.getTipsConfigList());
    }
}
